package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.CType;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Predmet obradio' from the chart.
 * 
 * @author Lab
 * 
 */
public class SubjectProcessedBy extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_ATTRIBUTE = "attribute";
    public static final String ONT_ACTOR = "actor";

    public SubjectProcessedBy(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        Ont.typeCheck(CType.RECORD);

        final Query query = new Query();
        final String[] result = query.select("attribute").type("attribute", CIDOC.E13.queryValue())
                .type("actor", CIDOC.E39.queryValue()).connect("attribute", CIDOC.P14.queryValue(), "actor")
                .connect("attribute", CIDOC.P21.queryValue(), CType.RECORD.queryValue()).label("actor", this.value)
                .find();

        final String itemURI = Item.formatItemURI(this.id);
        String attributeURI;
        if (result == null || result.length == 0) {
            attributeURI = Ont.formatURI(this.id, ONT_ATTRIBUTE);
            final String actorURI = Ont.formatURI(this.id, ONT_ACTOR, ONT_ATTRIBUTE);

            Ont.getOntology().createIndividual(CIDOC.E13.getFullValue(), attributeURI);
            Ont.getOntology().createIndividual(CIDOC.E39.getFullValue(), actorURI);

            Ont.getOntology().addLabel(actorURI, this.value);

            Ont.getOntology().connectIndividuals(attributeURI, CIDOC.P14.getFullValue(), actorURI);
            Ont.getOntology().connectIndividuals(attributeURI, CIDOC.P21.getFullValue(), CType.RECORD.getURI());
        } else {
            attributeURI = result[0];
        }

        Ont.getOntology().connectIndividuals(attributeURI, CIDOC.P140.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryValue = Item.formatItemQueryURI(this.id);
        final Query query = new Query();
        final String[] result = query.selectLabel().type("attribute", CIDOC.E13.queryValue())
                .connect("attribute", CIDOC.P140.queryValue(), itemQueryValue)
                .connect("attribute", CIDOC.P21.queryValue(), CType.RECORD.queryValue())
                .type("actor", CIDOC.E39.queryValue()).connect("attribute", CIDOC.P14.queryValue(), "actor")
                .findLabel("actor");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }

}
